﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace DmxInterface {
	
		static class NativeMethods {

			[DllImport("DasHard2006.dll")]
			public extern unsafe static int DasUsbCommand(int command, int param, byte[] bloc);

			public enum Command : int {
				Open = 1,
				Close = 2,
				DmxOutOff = 3,
				DmxOut = 4,
				PortRead = 5,
				PortConfig = 6,
				Version = 7,
				DmxIn = 8,
				Init = 9,
				Exit = 10
			}

			public enum ReturnCode : int {
				Ok = 1,
				NothingToDo = 2,
				Unassigned = 0, //This is NOT a valid value, and is used only in case of internal errors
				CommandError = -1,
				NotOpenError = -2,
				AlreadyOpenError = -12
			}

			/// <summary>Issues a simple Dmx command without parameters.</summary>
			/// <param name="command">Command to issue.</param>
			public static int IssueDmxCommand(NativeMethods.Command command, int deviceId) {
				int commandNum = (int)command + (deviceId * 100);
				int result;

				unsafe {
					result = NativeMethods.DasUsbCommand(commandNum, 0, null);
				}

				return result;
			}

			/// <summary>Issue a single Dmx command with data block.</summary>
			/// <param name="command">Command to issue.</param>
			/// <param name="data">Data block to pass in.</param>
			public static int IssueDmxCommand(NativeMethods.Command command, int deviceId, byte[] data) {
				int commandNum = (int)command + (deviceId * 100);
				int result;

				unsafe {
					result = NativeMethods.DasUsbCommand(commandNum, data.Length, data);
				}

				return result;
			}

		}

}
